local Harvest = GV:addState('Harvest')
function Harvest:enterState()
	print("entering harvest state")
	clearLoveCallbacks()

	function init_harvest()
		print("init harvest")
		harvest_maps[data.player.current_harvest] = {}

		harvest_maps[data.player.current_harvest].features = {}
		harvest_maps[data.player.current_harvest].items = {}
		harvest_maps[data.player.current_harvest].tiles = {}
		harvest_maps[data.player.current_harvest].enemies = {}
		harvest_maps[data.player.current_harvest].corpses = {}
		harvest_maps[data.player.current_harvest].meta_tiles = {}

		harvest_maps[data.player.current_harvest].layout = {}


		data.player.local_map = harvest_maps[data.player.current_harvest]
		local_map = data.player.local_map


		--print(local_map.name)

		mapped = build_map(harvest_maps[data.player.current_harvest].layout)

		refine_map(harvest_maps[data.player.current_harvest].layout)

		harvest_to_tiles(harvest_maps[data.player.current_harvest].layout,
			harvest_maps[data.player.current_harvest].tiles)

		--print(table.tostring(cur_map))

		local_map.layout = harvest_maps[data.player.current_harvest].layout
		p_random_spawn(data.player,
			harvest_maps[data.player.current_harvest].layout)
		spawn_stairs(data.player,
			harvest_maps[data.player.current_harvest].layout)
		spawn_nerd(harvest_maps[data.player.current_harvest].tiles)
		spawn_enemies(harvest_maps[data.player.current_harvest].enemies,
			harvest_maps[data.player.current_harvest].tiles)

		--print(table.tostring(local_map.enemies))


		local hn = append_strings("harvest", data.player.current_harvest)
		local_map.name = hn

		--table.insert(harvest_maps, local_map)



	end

	if data.player.times_harvesting > 0
		and harvest_maps[data.player.current_harvest] ~= nil then
		data.player.times_harvesting = data.player.times_harvesting + 1
		cur_map = harvest_maps[data.player.current_harvest].layout
		print(table.tostring(cur_map))
		if data.player.hup == false then
			p_load_harvest("hup")
		else
			p_load_harvest()
		end

	elseif harvest_maps[data.player.current_harvest] == nil then
		data.player.times_harvesting = data.player.times_harvesting + 1
		print("map == nil")

		init_harvest()


	else
		print("data.player.times_harvesting <= 0")
		data.player.times_harvesting = data.player.times_harvesting + 1
		init_harvest()
	end



	local exp_count = 0
	local frame_count = 0
	function love.update(dt)
		TEsound.cleanup()

		exp_count = exp_count+1
		if exp_count == 15 then
			explore_map(local_map.tiles, local_map.features,
				local_map.enemies, local_map.items, local_map.corpses)

			exp_count = 0
		end

		frame_count = frame_count + 1
		if frame_count == 120 then
			clear_console()
			--npc_tidy(maps.town.npcs)
			--p_tidy()
			frame_count = 0
		end

		g_turn()





	end

	function love.draw()
		draw_harvest()
		draw_features()
		draw_corpses()
		draw_items()
		draw_enemies(local_map.enemies)
		draw_player()
		draw_hud()
		draw_grenade(gren_thrown_x, gren_thrown_y, grenade_thrown)
		draw_narrative()

	end

	function love.keypressed(key, unicode)
		--print(table.tostring(cur_map))
		if data.player.turn == true then
			basic_controls(key, unicode, data.player.local_map.layout, "tiled")

			if key == "x" then
			local do_once = false
			print("current harvest: ", data.player.current_harvest)
				for i = 1, #local_map.features do
					if local_map.features[i] ~= nil then
						if data.player.x == local_map.features[i].x
							and data.player.y == local_map.features[i].y then
							if do_once == false then
								if local_map.features[i].name == "stairs_up"
									and data.player.current_harvest == 1 then
									do_once = true
									data.player.current_harvest = data.player.current_harvest-1
									print("current harvest : ", data.player.current_harvest)
									harvest_maps[data.player.current_harvest] = local_map
									data.player.hup = false
									p_goto_town()

								elseif local_map.features[i].name == "stairs_up" then

									data.player.current_harvest = data.player.current_harvest-1
									do_once = true
									print("subtracted from harvest : ", data.player.current_harvest)
									data.player.hup = false
									gv:gotoState('Temp')


								elseif local_map.features[i].name == "stairs_down" then

									if harvest_maps[data.player.current_harvest+1] ~= nil then
										data.player.current_harvest = data.player.current_harvest +1
										data.player.hup = true
										gv:gotoState('Temp')
									else

										print("ready to make new harvest")
										harvest_maps[data.player.current_harvest] = local_map
										data.player.current_harvest = data.player.current_harvest+1
										data.player.hup = true
										gv:gotoState('Temp')

									end


								end
							end
						end
					end
				end
			end


		end
	end

	function draw_harvest()

		for i = 1, #local_map.tiles do
			draw_in_fov(local_map.tiles[i])

		end

	end

end

function Harvest:exitState()
	print("exiting harvest state")
end


local Temp = GV:addState('Temp')
function Temp:enterState()
	print("entering temp state")
	gv:gotoState('Harvest')

end

function Temp:exitState()
	print("exiting temp state")
end
